<template>
  <div>
    <div class="bg">
      <div class="proflieCard">
        <div class="logo1">
          <img :src="avatar" alt="" class="logo2">
        </div>
        <p class="name">{{ username }}</p>
        <div class="buttons">
          <button @click="back" class="button1" onclick="">返回</button>
          <button @click="change" class="button2" onclick="">上传</button>
        </div>
      </div>
    </div>
    <div class="background" id="background">
      <img src="../assets/img/background.png"/>
    </div>
    <div class="background2" id="background2">
      <img src="../assets/img/background2.png"/>
    </div>
    <my-upload
        @crop-success="cropSuccess"
        @crop-upload-success="cropUploadSuccess"
        @crop-upload-fail="cropUploadFail"
        v-model="isShow"
        :width="200"
        :height="200"
        :params="avatarObj"
        img-format="png"
        langType='zh'
        :noRotate='false'
        field="Avatar1"
        :url="url"
    ></my-upload>
  </div>
</template>

<script>
import myUpload from "vue-image-crop-upload";
export default {
  created() {
    this.username = this.$store.getters.userInfo.nickName
  },
  components: {
    "my-upload": myUpload
  },
  name: "updateAvatar",
  data() {
    return {
      username: '',
      avatar: '/file/download/' + this.$store.getters.userInfo.avatar,
      isShow: false,
      imagecropperKey: 0,
      url: '/file/upload',
      avatarObj: 'avatar'
    }
  },
  methods: {
    load2D_bg() {
      var background = document.getElementById("background");
      var range = 40;
      var calcValue = function calcValue(a, b) {
        return (a / b * range - range / 2).toFixed(1);
      };
      var timeout = void 0;
      document.addEventListener('mousemove',
          function (_ref) {
            var x = _ref.x,
                y = _ref.y;
            if (timeout) {
              window.cancelAnimationFrame(timeout);
            }
            timeout = window.requestAnimationFrame(function () {
              var yValue = calcValue(y, window.innerHeight);
              var xValue = calcValue(x, window.innerWidth);


              background.style.backgroundPositionX = xValue * 1 - 200 + "px ";
              background.style.backgroundPositionY = (-yValue * 0.75 - 50) + "px";
            })
          }, false);

    },

    load2D_bg2() {
      var background = document.getElementById("background2");
      var range = 40;
      var calcValue = function calcValue(a, b) {
        return (a / b * range - range / 2).toFixed(1);
      };
      var timeout = void 0;
      document.addEventListener('mousemove',
          function (_ref) {
            var x = _ref.x,
                y = _ref.y;
            if (timeout) {
              window.cancelAnimationFrame(timeout);
            }
            timeout = window.requestAnimationFrame(function () {
              var yValue = calcValue(y, window.innerHeight);
              var xValue = calcValue(x, window.innerWidth);

              background.style.backgroundPositionX = xValue * 1 - 200 + "px ";
              background.style.backgroundPositionY = (-yValue * 0.75 - 50) + "px";
            })
          }, false);

    },

    load2D_bg2_2() {
      var background = document.getElementById("background2");
      document.onmousemove = function () {
        console.log(window.outerHeight);
        background.style.opacity = 1 - (getMousePos() / window.outerHeight * 2.5);
      }
    },

    getMousePos(event) {
      var e = event || window.event;
      var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
      var y = e.pageY || e.clientY + scrollY;
      return y;
    },
    cropSuccess(imgDataUrl, field) {
      console.log("-------- crop success --------");
      console.log(imgDataUrl);
      //把头像设置成上传的图片
      this.avatar = imgDataUrl;
      console.log(field);
    },
    //上传成功回调
    cropUploadSuccess(res, originPicName) {
      //res是后端返回的结果，originPicName是后端接收到图片的名字
      console.log("-------- upload success --------");
      console.log(res);
      this.imagecropperShow = false;
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    //上传失败回调
    cropUploadFail(status, field) {
      console.log("-------- upload fail --------");
      console.log("上传失败状态" + status);
      console.log("field: " + field);
    },
    change() {
      this.isShow = true

    },
    back() {
      this.$router.go(-1)
    }
  }
}

</script>

<style scoped>
.bg {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-height: 100vh;

  position: absolute;
  z-index: 0;

  display: flex;
  justify-content: center;
  align-items: center;
}

.background {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-height: 100vh;

  position: absolute;
  z-index: -5;

  display: flex;
  justify-content: center;
  align-items: center;
}

.background2 {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  position: absolute;
  z-index: -4;
}

@media screen and (max-width: 768px) {
  .background {
    background-size: cover;
  }

  .background2 {
    background-size: cover;
  }
}

@media screen and (max-width: 1000px) {
  .background {
    background-size: 130%;
  }

  .background2 {
    background-size: 130%;
  }
}

@media screen and (max-width: 2333333px) {
  .background {
    background-size: 130%;
  }

  .background2 {
    background-size: 130%;
  }
}


.profileCard {


  z-index: 0;

}

.logo1 {

  margin: auto;
  margin-top: -100px;
  width: 200px;
  height: 200px;
  /*background-color: rgba(255, 255, 255, 0.4);
  border-radius: 50%;
  box-shadow: 0px 0px 10px #ffffff;
  */
}

.logo2 {
  width: 180px;
  height: 180px;
  margin: 5px 5px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.8);
  box-shadow: 0px 0px 12px rgba(255, 255, 255, 0.9);
  transition: all 1s;
  /*box-shadow: 0px 0px 10px #ffffff;*/
}

.logo2:hover {
  transform: rotate(360deg);
  box-shadow: 0px 0px 50px rgba(255, 255, 255, 1);

  transition: all 1s;
  /*box-shadow: 0px 0px 10px #ffffff;*/
}

.name {
  margin-top: 5px;
  text-align: center;
  font-size: 30px;
  color: white;
  font-weight: 500;
  text-shadow: 0px 0px 12px white;
  transition: all 0.5s;
}

.name:hover {
  text-shadow: 0px 0px 30px white;

  transition: all 0.5s;
}


.slogan2 {
  text-align: center;
  color: rgba(255, 255, 255, 1);
  font-size: 16px;
  max-width: 500px;
}

.buttons {
  margin-top: 15px;
  margin-right: auto;
  margin-left: auto;
  width: 500px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.button1 {
  margin-left: 10px;
  margin-right: 10px;
  height: 50px;
  width: 200px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 25px;
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
  font-size: 16px;
  transition: all 0.5s;
}

.button1:hover {
  background-color: rgba(255, 255, 255, 0.4);
  color: #ffffff;
  box-shadow: 0px 0px 12px #ffffff;
  transition: all 0.5s;
}

.button2 {
  margin-left: 10px;
  margin-right: 10px;
  height: 50px;
  width: 200px;
  border: 1px solid rgba(56, 211, 159, 0.4);
  border-radius: 25px;
  background-color: rgba(56, 211, 159, 0.1);
  color: white;
  font-size: 16px;
  transition: all 0.5s;
}

.button2:hover {
  background-color: rgba(56, 211, 159, 0.4);
  color: #ffffff;
  box-shadow: 0px 0px 12px #ffffff;
  transition: all 0.5s;
}

.footer {
  position: absolute;

  left: 50%;
  transform: translateX(-50%);
  bottom: 0px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 10px;
}

.footer2 {
  text-align: center;
}

a {
  color: rgba(255, 255, 255, 0.8);
}
</style>